package com.hrt.manage.role.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hrt.bean.Role;
import com.hrt.bean.RoleAccess;
import com.hrt.bean.ViewResource;
import com.hrt.manage.access.dao.AccessDao;
import com.hrt.manage.access.service.AccessService;
import com.hrt.manage.menus.service.ViewResourceService;
import com.hrt.manage.role.dao.RoleDao;
import com.hrt.manage.role.service.RoleService;

@Service
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleDao dao;

	@Autowired
	private ViewResourceService viewResourceService;

	@Autowired
	private AccessService accService;

	@Autowired
	private AccessDao accDao;

	@Override
	public List<Role> queryByPage(int pageIndex, int pageSize) {
		// TODO Auto-generated method stub
		List<Role> list = new ArrayList<Role>();
		list = dao.queryByPage(pageIndex, pageSize);

		return list;
	}

	@Override
	public Long totalCount() {
		// TODO Auto-generated method stub
		return dao.totalCount();
	}

	@Override
	public Long totalCount(Map condition) {
		// TODO Auto-generated method stub
		return dao.totalCount(condition);
	}

	@Override
	public List<Role> queryByPage(List<Map<String, Object>> conditions,
			Integer pageIndex, Integer pageSize) {
		// TODO Auto-generated method stub
		List<Role> results = new ArrayList<Role>();
		results = dao.queryByPage(conditions,pageIndex, pageSize);
		return results;
	}

	@Override
	@Transactional
	public Long[] deleteById(Long[] idArr) {
		// TODO Auto-generated method stub
		List<Long> delArr = new ArrayList<Long>();
		if (idArr != null && idArr.length > 0) {
			for (Long id : idArr) {
				Role c = dao.getById(id);
				c.setIsdelete(new Integer(1));
				dao.updateEntity(c);
				delArr.add(id);
			}
		}
		return delArr.toArray(new Long[delArr.size()]);
	}

	@Override
	public String queryRoleByPinYin(String pinyin) {
		// TODO Auto-generated method stub
		return dao.queryRoleByPinYin(pinyin);
	}

	@Override
	public Timestamp getDate() {
		// TODO Auto-generated method stub
		return dao.getDate();
	}

	@Override
	@Transactional
	public Long saveRole(Role role) {
		// TODO Auto-generated method stub
		return dao.saveEntity(role);
	}

	@Override
	public Role getRoleById(Long roleId) {
		// TODO Auto-generated method stub
		return dao.getRoleByid(roleId);
	}

	@Override
	@Transactional
	public void updateRole(Role role) {
		// TODO Auto-generated method stub
		dao.updateEntity(role);
	}

	@Override
	public List<Role> listRole() {
		// TODO Auto-generated method stub
		List<Role> results = new ArrayList<Role>();
		results = dao.listRole();
		return results;
	}

	@Override
	public List<Role> queryRoleWithOutUsers(Long userId) {
		List<Role> results = new ArrayList<Role>();
		results = dao.queryRoleWithOutUsers(userId);
		return results;
	}

	@Override
	public List<Role> getRoleByIds(Long[] ids) {
		// TODO Auto-generated method stub
		List<Role> results = new ArrayList<Role>();
		results = dao.getRoleByid(ids);
		return results;
	}

	@Transactional
	@Override
	public void saveAccInfor(Long roleId, Integer scope, List<Long> resList) {
		// TODO Auto-generated method stub
		List<ViewResource> oldResource = new ArrayList<ViewResource>();
		oldResource = accService.queryAccByScope(roleId, scope);
		if (oldResource != null && !oldResource.isEmpty()) {
			accService.deleteOldResource(oldResource);
		}
		Role role = this.dao.getRoleByid(roleId);
		if (role != null) {
			for (Long resId : resList) {
				ViewResource vr = viewResourceService
						.getViewResourceById(resId);

				if (vr != null) {
					RoleAccess roleAcc = new RoleAccess();
					roleAcc.setIsdelete(0);
					roleAcc.setResId(vr.getId());
					roleAcc.setResName(vr.getVrName());
					roleAcc.setRoleId(role.getId());
					accDao.saveEntity(roleAcc);
				}
			}
		}

	}

}
